﻿using System;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	//格式化所有Optional， Repeating 等Section下的按钮。
	//主要是添加onclick事件，并添加按钮图片。
	class XSNViewFormatPlaceholderButtonWorker
	{
		
		public static void  formatPlaceholderButton(View view)
		{
			string xdPrefix = view.XdPrefix;
			string xdActionName = NamespaceUtil.toFullName(xdPrefix, JetConstant.XD_action);
			
			//找到所有optionalPlaceholder。
			XmlElement root = view.Doc.DocumentElement;
			string placeholderFilter = "//*[@class='optionalPlaceholder']";
			XmlNodeList placeholderList = DOMUtil.selectNodes(root, placeholderFilter, 0, view.NsResolver);
			int index = 0;
			foreach (XmlElement placeholderElement in placeholderList)
			{
				//因为客户端ValueChanging延迟机制的需要，所有placeholer都应该具有Id。
				placeholderElement.SetAttribute( JetConstant.HTML_id, JetConstant.InfoJetPlaceholder + index );
				index ++;

				//添加onclick事件，禁用contextMenu。
				DOMUtil.setEventAttribute(placeholderElement, "onclick", "InfoJet_OnPlaceholderClick(event,this);");
				//测试使用TAB移动到placeholder上时,按下回车,执行placeholder。Placeholder keydown.xsn
				DOMUtil.setEventAttribute(placeholderElement, "onkeydown", "InfoJet_OnPlaceholderKeydown(event,this);");
				DOMUtil.setEventAttribute(placeholderElement, "oncontextmenu", "InfoJet_DisableContextMenu(event);");
				
				//以下是根据不同类型的Section添加不同的图片。
				XmlElement transImageElement = view.Doc.CreateElement("img");
				transImageElement.SetAttribute("src", view.Manifest.ImageUri.Transparent);
				XmlElement arrowImageElement = view.Doc.CreateElement("img");
				
				string xdAction = placeholderElement.GetAttribute(xdActionName);
				if (xdAction.Length <= 0)
				{
					arrowImageElement.SetAttribute("src", view.Manifest.ImageUri.OptionalArrow);
				}
				else
				{
					if (xdAction.StartsWith(JetConstant.CMP_xOptional))
					{
						arrowImageElement.SetAttribute("src", view.Manifest.ImageUri.OptionalArrow);
					}
					else if (xdAction.StartsWith(JetConstant.CMP_xCollection))
					{
						arrowImageElement.SetAttribute("src", view.Manifest.ImageUri.CollectionArrow);
					}
					//CID 0293 //2009.05.28 //Begin
					else if (xdAction.StartsWith(JetConstant.CMP_xTextList))
					{
						arrowImageElement.SetAttribute("src", view.Manifest.ImageUri.CollectionArrow);
					}
					//CID 0293 //2009.05.28 //End
				}
				
				XmlNodeList nodeList = placeholderElement.ChildNodes;
				if (nodeList.Count > 0)
				{
					XmlNode firstNode = nodeList[ 0 ];
					placeholderElement.InsertBefore(arrowImageElement, firstNode);
					placeholderElement.InsertBefore(transImageElement, firstNode);
				}
				else
				{
					placeholderElement.AppendChild(transImageElement);
					placeholderElement.AppendChild(arrowImageElement);
				}

				//CID 0347 //2009.07.13 //Begin
				placeholderElement.SetAttribute( "onmousedown", "javascript:InfoJet_OnButtonMouseDown(event, this);" );
				placeholderElement.SetAttribute( "onfocus", "javascript:InfoJet_OnButtonFocus(event, this);" );
				//CID 0347 //2009.07.13 //End
			}

			
			//格式化XmlToEditButton，XmlToEditButton是InfoJetServce.config中定义的和placeholder button功能一样的普通按钮。
			if( view.Manifest.FormConfiguration.XmlToEditButtonList != null )
			{
				foreach( XmlToEditButton button in view.Manifest.FormConfiguration.XmlToEditButtonList )
				{
					string ctrlIdFilter = "//*[@" + NamespaceUtil.toFullName( xdPrefix, JetConstant.XD_CtrlId ) + "='" + button.Id + "']";
					XmlElement buttonElement = (XmlElement)root.SelectSingleNode( ctrlIdFilter, view.NsResolver );
					if( buttonElement != null )
					{
						buttonElement.SetAttribute( JetConstant.XD_xmlToEdit, JetConstant.URI_XD, button.XmlToEdit );
						buttonElement.SetAttribute( JetConstant.XD_action, JetConstant.URI_XD, button.Action );
						buttonElement.SetAttribute( "onclick", "InfoJet_OnPlaceholderClick(event,this);" );
					}
				}
			}
		}
	}
}